package com.athena.service.faculty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.athena.api.faculty.IFacultyService;
import com.athena.dal.compositeEntities.FacultyRoutines;
import com.athena.dal.dao.FacultyDao;
import com.athena.dal.dao.GenericDao;
import com.athena.dal.entities.Class;
import com.athena.dal.entities.Faculty;
import com.athena.dal.entities.Subject;
import com.athena.dal.entities.User;

/**
 * 
 * @author Ajit.Kumar
 *
 */
public class FacultyService implements IFacultyService {
	
	@Autowired
	private FacultyDao facultyDao;
	@Autowired
	private GenericDao genericDao;

	/**
	 * @param facultyDao the facultyDao to set
	 */
	public void setFacultyDao(FacultyDao facultyDao) {
		this.facultyDao = facultyDao;
	}

	@Override
	public Faculty getFaculty(int facultyId) {
		if(facultyId <= 0){
			throw new IllegalArgumentException("Faculty Id is non-positive");
		}
		
		// Search in DB using faculty DAO
		Faculty faculty = facultyDao.findById(Faculty.class, facultyId);
		
		// if Faculty information is found in DB, return the Faculty object else 
		// return null.
		
		return faculty;
	}

	@Override
	public List<Faculty> allFaculties(String departmentId) {
		if(departmentId == null || departmentId.isEmpty()){
			throw new IllegalArgumentException("Either DepartmentId is Null or Empty");
		}
		//Search in DB using faculty DAO 
		
		// if Faculties information is found in DB, return the List<Faculty> else 
		// return null.
		
		return null;
	}

	@Override
	public List<Subject> teaches(int facultyId) {
		if(facultyId <= 0){
			throw new IllegalArgumentException("Invalid faculty id:" + facultyId);
		}
		//Make a join between Teaches and Subject table with subjectId as join parameter 
		// and fetch List<Subjects>.
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("facultyId", facultyId);
		System.out.println("Finding subject for facutly id:" + facultyId);
		List<Subject> listSubjects = genericDao.findAllByNameQuery(Subject.class, "Teach.findByFacultyId", parameters);
		System.out.println("List of subject:::" + listSubjects);
		return listSubjects;
	}

	@Override
	public List<Subject> hasTaught(int facultyId) {
		if(facultyId <= 0){
			throw new IllegalArgumentException("Faculty Id is non-positive");
		}
		// get list of subjects from previous Academic years.
		//TODO Needs to discuss how this information can be fetched.
		
		return null;
	}

	@Override
	public Class classTeacherOf(int facultyId) {
		if(facultyId <= 0){
			throw new IllegalArgumentException("Faculty Id is non-positive");
		}
		// Query class Table with facultyId as class teacher.
		
		// If there is no information retrieved return null.
		return null;
	}

	@Override
	public List<FacultyRoutines> getRoutine(User user, long start, long end) {
		/*if(facultyId <= 0){
			throw new IllegalArgumentException("Faculty Id is non-positive");
		}
		
		if(startDate == null){
			throw new IllegalArgumentException("Start date is null");
		}
		
		if(endDate == null){
			throw new IllegalArgumentException("End date is null");
		}
		*/// Call Routine Service to get the required routines.
		
		// If no routine is found return an empty List.
		return facultyDao.getMyRoutines(user, start, end);
	}

}
